using System;
using System.IO;
using SmartBoard.Core.Data;
using SmartBoard.Core.Element;
using SmartBoard.Core.Networking.Client;
using SmartBoard.Core.Server;

namespace SmartBoard.BackGround.BoardElement
{
    public class BackGroundNetworkingModule : INetworkServiceModule
    {
        private readonly IBoardElement _element;
        private IClientDataWriter _dataWriter;

        public BackGroundNetworkingModule(IBoardElement element)
        {
            _element = element;
        }

        #region Implementation of INetworkServiceModule

        public byte ModuleId
        {
            get { return 1; }
        }

        public void AcceptMessage(IServerMessage message)
        {
            if(message.MessageTargetModuleId == this.ModuleId)
            {
                _element.CommandManager.AcceptCommandMessage(message.MessageId, message.Payload);
            }
        }

        public void WriteMessage(IServerMessage message)
        {
            _dataWriter.Write(message);
        }

        public void AcceptWriter(IClientDataWriter writer)
        {
            _dataWriter = writer;
        }

        #endregion
    }
}